package com.glsc.ngateway.common.base.converter.platform;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.excel.util.CollectionUtils;
import com.glsc.ngateway.common.api.platform.dto.user.PlatformUserDto;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.Role;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.Url;
import com.glsc.ngateway.common.base.domain.mysql.gateway.platform.User;

import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author xuchang
 */
public class PlatformUserConverter {
    public static PlatformUserDto do2Dto(User domain) {
        return Objects.isNull(domain) ? null : PlatformUserDto.builder()
                .id(domain.getId())
                .enabled(domain.getEnabled())
                .deleted(domain.getDeleted())
                .username(domain.getUsername())
                .password(domain.getPassword())
                .name(domain.getName())
                .remark(domain.getRemark())
                .userType(domain.getUserType())
                .email(domain.getEmail())
                .userid(domain.getUserid())
                .userDepartment(domain.getUserDepartment())
                .departmentId(domain.getDepartmentId())
                .roles(CollectionUtil.isEmpty(domain.getRoles()) ? Collections.EMPTY_SET : domain.getRoles().stream().map(Role::getName).collect(Collectors.toSet()))
                .urls(CollectionUtil.isEmpty(domain.getUrls()) ? Collections.EMPTY_SET : domain.getUrls().stream().map(UrlConverter::do2Dto).collect(Collectors.toSet()))
                .auths(CollectionUtil.isEmpty(domain.getUrls()) ? Collections.EMPTY_SET : domain.getUrls().stream().filter(url -> url.getDeleted() == 0).map(Url::getName).collect(Collectors.toSet()))
                .department(domain.getUserDepartment())
                .disable(domain.getDisable())
                .build();
    }

    public static List<PlatformUserDto> do2Dto(List<User> domain) {
        return CollectionUtils.isEmpty(domain) ? java.util.Collections.emptyList() : domain.stream().map(PlatformUserConverter::do2Dto).collect(Collectors.toList());
    }

    public static User dto2Do(PlatformUserDto dto) {
        return Objects.isNull(dto) ? null : User.builder()
                .id(dto.getId())
                .enabled(dto.getEnabled())
                .deleted(dto.getDeleted())
                .username(dto.getUsername())
                .password(dto.getPassword())
                .name(dto.getName())
                .remark(dto.getRemark())
                .userType(dto.getUserType())
                .email(dto.getEmail())
                .userid(dto.getUserid())
                .userDepartment(dto.getUserDepartment())
                .departmentId(dto.getDepartmentId())
                .disable(dto.getDisable())
//                .roles(CollectionUtil.isEmpty(dto.getRoles()) ? Collections.EMPTY_SET : dto.getRoles().stream().map(Role::getName).collect(Collectors.toSet()))
//                .urls(CollectionUtil.isEmpty(dto.getUrls()) ? Collections.EMPTY_SET : dto.getUrls().stream().map(UrlConverter::do2Dto).collect(Collectors.toSet()))
//                .auths(CollectionUtil.isEmpty(dto.getUrls()) ? Collections.EMPTY_SET : dto.getUrls().stream().filter(url -> url.getDeleted() == 0).map(Url::getName).collect(Collectors.toSet()))
//                .department(dto.getUserDepartment())
                .build();
    }

    public static List<User> dto2Do(List<PlatformUserDto> dto) {
        return CollectionUtils.isEmpty(dto) ? java.util.Collections.emptyList() : dto.stream().map(PlatformUserConverter::dto2Do).collect(Collectors.toList());
    }
}
